﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace DAL
{
    public class PhanQuyenProvider
    {
        //public static List<PhanQuyen> LayDanhSachPhanQuyen()
        //{
        //    var lst = new List<PhanQuyen>();
        //    using (var context = new QLTTTHEntities())
        //    {
        //        var query = (from u in context.PhanQuyens select u);

        //        foreach (var row in query)
        //        {
        //            var phanQuyen = new PhanQuyen();
        //            phanQuyen.MaNhomND = row.MaNhomND;
        //            phanQuyen.MaNhomQuyen = row.MaNhomQuyen;
        //            phanQuyen.KichHoat = row.KichHoat;

        //            lst.Add(phanQuyen);
        //        }
        //    }

        //    return lst;
        //}

        public static List<PhanQuyenInfo> LayDanhSachPhanQuyen()
        {
            var lst = new List<PhanQuyenInfo>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from pq in context.PhanQuyens
                             join nd in context.NhomNguoiDungs
                             on pq.MaNhomND equals nd.MaNhomND
                             join nq in context.NhomQuyens
                             on pq.MaNhomQuyen equals nq.MaNhomQuyen
                             select new
                             {
                                 MaNhomNguoiDung = pq.MaNhomND,
                                 MaNhomQuyen = pq.MaNhomQuyen,
                                 TenNhom = nd.TenNhom,
                                 MoTa = nd.MoTa,
                                 TrangThai = pq.KichHoat

                             }
                            ).ToList();

                foreach (var row in query)
                {
                    var phanQuyen = new PhanQuyenInfo();
                    phanQuyen.MaNhomNguoiDung = row.MaNhomNguoiDung;
                    phanQuyen.MaNhomQuyen = row.MaNhomQuyen;
                    phanQuyen.TenNhom = row.TenNhom;
                    phanQuyen.MoTa = row.MoTa;
                    phanQuyen.TrangThai = (bool)row.TrangThai;

                    lst.Add(phanQuyen);
                }
            }

            return lst;
        }

        public static PhanQuyen LayPhanQuyenTheoMaNhomNguoiDung(int maNhomND)
        {

            using (var context = new QLTTTHEntities())
            {
                var query = context.PhanQuyens
                    .Where(t => t.MaNhomND.Equals(maNhomND))
                    .First();
                
                var pq = new PhanQuyen();
                pq.MaNhomND = query.MaNhomND;
                pq.MaNhomQuyen = query.MaNhomQuyen;
                pq.KichHoat = query.KichHoat;

                return pq;            
            }

        }


        public static bool XoaPhanQuyen(int maNhomND, int maNhomQ)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {

                    var query = (from u in context.PhanQuyens
                                 where u.MaNhomND.Equals(maNhomND)
                                 select u)
                                .Where(t => t.MaNhomQuyen.Equals(maNhomQ))
                                .First();

                    context.PhanQuyens.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CapNhatPhanQuyen(PhanQuyen phanQuyen)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.PhanQuyens
                                 where u.MaNhomND.Equals(phanQuyen.MaNhomND)
                                 select u)
                                .Where(t => t.MaNhomQuyen.Equals(phanQuyen.MaNhomQuyen))
                                .First();
                    query.KichHoat = phanQuyen.KichHoat;

                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool ThemPhanQuyen(PhanQuyen phanQuyen)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    context.PhanQuyens.Add(phanQuyen);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
            
        }
    }
}
